<?php
namespace app\home\controller;
use app\common\model\Ucpaas as UcpaasModel;
use app\home\model\Login as loginModel;
class Login extends Home
{

  //验证码发送时间
  // public $time = '';
  //验证码存放
  // public $param;
public function send(){
    if(request()->isPost()){
    $phone = input('post.phone');
        //初始化必填
        $options['accountsid']='ffc7d537e8eb86b6ffa3fab06c77fc02'; //填写自己的
        $options['token']='894cfaaf869767dce526a6eba54ffe52'; //填写自己的
        // //初始化 $options必填
        $ucpass = new UcpaasModel($options);
                
                //随机生成6位验证码
        $authnum="";
        srand((double)microtime()*1000000);//create a random number feed.
        $ychar="0,1,2,3,4,5,6,7,8,9";
        $list=explode(",",$ychar);
        for($i=0;$i<4;$i++){
        $randnum=rand(0,9); // 10+26;
        $authnum.=$list[$randnum];
        }
        //短信验证码（模板短信）,默认以65个汉字（同65个英文）为一条（可容纳字数受您应用名称占用字符影响），超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。

        $appId = "33553da944fb487089dadb16a37c53cc";  //填写自己的
        // $phone = '1';
       // $to = '13137970053';
        $to =  $phone;
        $templateId = "173289";
        $param= $authnum;
        
       $arr=$ucpass->templateSMS($appId,$to,$templateId,$param);
        $json = json_decode($arr,true);
        // pr($json);

        // resp['resp']['templateSMS']['createDate']
        if (substr($arr,21,6) == 000000) {
            $time = $json['resp']['templateSMS']['createDate'];
            // $time='20171016111650';
            $num = '';
            $res = loginModel::cats($phone);
            // pr($res);die;
            if($res){
               $num = $res['login_num'];
               ++$num;
                $data['login_time'] = $time;
               $data['login_code'] = $param;
                 $data['login_num'] = $num;
                  $data['login_phone'] = $phone;
                 $data['login_id'] = $res['login_id'];
                  $data['login_state'] = 0;
                 // pr($data);die;
                 // pr($where);die;
                if(loginModel::updates($data)){
                    return  1;
                 }else{
                    return 0;
                 }
            }else{
                $num =1;
                $data['login_phone'] = $phone;
                $data['login_time'] = $time;
                $data['login_code'] = $param;
                $data['login_num'] = $num;
                $data['login_state'] = 0;
                 // pr($data);die;
                if(loginModel::login($data)){
                    return 1;
                 }else{
                    return 0;
                 }
                
            }
            //如果成功就，这里只是测试样式，可根据自己的需求进行调节
    
      
         

            
        }else{
        //     //如果不成功
            return 0;
            
        }
        
        
    }
}
function login(){
      $time =  time();
  
      // echo $this->time;die;
     $code = input('post.code');
     $phone=input('post.phone');

    $res = loginModel::cat($phone);

     if($res){
      if($res['login_code'] != $code){
        
            return 3;
       }else if((strtotime($res['login_time'])+180)<$time){
               
                return 0;
       
        }else if($res['login_code'] == $code){
             session('PHONE',$res['login_phone']);
             $data['login_id'] = $res['login_id'];
             $data['login_state'] = 1;
             loginModel::updates($data);
            return 1;
        }else{
            return 2;
        }

        }else{
            return 3;
        }
    }
    public function logout(){
        if(request()->isGet()){
           // $sess = input('session');
        // echo 1;
           session('PHONE',null);
            if(!session('PHONE')){
                return 1;
            }
           // pr(session('session'));
        }
    }
}

